package cn.bytecollege.filter;

import cn.bytecollege.constant.Resource;
import cn.bytecollege.entity.Room;
import cn.bytecollege.entity.UserManage;
import cn.bytecollege.service.RoomService;
import cn.bytecollege.service.UserMessageService;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author LH
 * @time 2021-10-28 18:36
 */
@WebFilter(filterName = "UserMessageFilter", servletNames = {"UserMessageAddController", "UserMessageChangeController"})
public class UserMessageFilter implements Filter {
    private UserMessageService userMessageService=null;
    private RoomService roomService=null;
    public UserMessageFilter(){
        roomService=new RoomService();
        userMessageService=new UserMessageService();
    }
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        String msg = null;
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        String method = req.getMethod();
        if (method.equalsIgnoreCase("post")) {
            if (uri.contains("userMessageAdd")) {
                String username = req.getParameter("username");
                String phone = req.getParameter("phone");
                String idNumber = req.getParameter("idNumber");
                String roomNumber = req.getParameter("roomNumber");
                if (username == null || username.equals("")) {
                    msg = "用户名不能为空！";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageAdd.jsp")
                            .forward(request, response);
                    return;
                }
                if (phone == null || phone.equals("")) {
                    msg = "电话号不能为空！";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageAdd.jsp")
                            .forward(request, response);
                    return;
                }
                if (idNumber == null || idNumber.equals("")) {
                    msg = "身份证号码不能为空！";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageAdd.jsp")
                            .forward(request, response);
                    return;
                }
                if (roomNumber == null || roomNumber.equals("")) {
                    msg = "房间号不能为空！";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageAdd.jsp")
                            .forward(request, response);
                    return;
                }
                Pattern usernamePattern = Pattern.compile("^[\\u4e00-\\u9fa5]{0,}$|^[A-Za-z]+$");
                Matcher usernameMatcher = usernamePattern.matcher(username);
                if (!usernameMatcher.matches()) {
                    //说明与要求的输入格式不匹配
                    msg = "姓名应为汉字或英文";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageAdd.jsp").forward(request, response);
                    return;
                }
                Pattern phonePattern = Pattern.compile("^1[0-9]{10}$");
                Matcher phoneMatcher = phonePattern.matcher(phone);
                if (!phoneMatcher.matches()) {
                    //说明与要求的输入格式不匹配
                    msg = "电话号码格式不正确";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageAdd.jsp").forward(request, response);
                    return;
                }
                Pattern idNumberPattern = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)");
                Matcher idNumberMatcher = idNumberPattern.matcher(idNumber);
                if (!idNumberMatcher.matches()) {
                    //说明与要求的输入格式不匹配
                    msg = "身份证号码格式不正确";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageAdd.jsp").forward(request, response);
                    return;
                }
                Room room = null;
                room = roomService.findByRoomNumber(Integer.valueOf(roomNumber));
                if (room == null) {
                    msg = "该房间不存在或已被入住";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageAdd.jsp").forward(request, response);
                    return;
                }
            }
            if (uri.contains("userMessageChange")) {
                String id=req.getParameter("id");
                String username = req.getParameter("username");
                String phone = req.getParameter("phone");
                String idNumber = req.getParameter("idNumber");
                String roomNumber = req.getParameter("roomNumber");
                UserManage userManage=new UserManage();
                userManage= userMessageService.findById(Integer.valueOf(id));
                request.setAttribute("userManage",userManage);
                if (username == null || username.equals("")) {
                    msg = "用户名不能为空！";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageChange.jsp")
                            .forward(request, response);
                    return;
                }
                if (phone == null || phone.equals("")) {
                    msg = "电话号不能为空！";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageChange.jsp")
                            .forward(request, response);
                    return;
                }
                if (idNumber == null || idNumber.equals("")) {
                    msg = "身份证号码不能为空！";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageChange.jsp")
                            .forward(request, response);
                    return;
                }
                if (roomNumber == null || roomNumber.equals("")) {
                    msg = "房间号不能为空！";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageChange.jsp")
                            .forward(request, response);
                    return;
                }
                Pattern usernamePattern = Pattern.compile("^[\\u4e00-\\u9fa5]{0,}$|^[A-Za-z]+$");
                Matcher usernameMatcher = usernamePattern.matcher(username);
                if (!usernameMatcher.matches()) {
                    //说明与要求的输入格式不匹配
                    msg = "姓名应为汉字或英文";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageChange.jsp").forward(request, response);
                    return;
                }
                Pattern phonePattern = Pattern.compile("^1[0-9]{10}$");
                Matcher phoneMatcher = phonePattern.matcher(phone);
                if (!phoneMatcher.matches()) {
                    //说明与要求的输入格式不匹配
                    msg = "电话号码格式不正确";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageChange.jsp").forward(request, response);
                    return;
                }
                Pattern idNumberPattern = Pattern.compile("(^\\d{15}$)|(^\\d{18}$)|(^\\d{17}(\\d|X|x)$)");
                Matcher idNumberMatcher = idNumberPattern.matcher(idNumber);
                if (!idNumberMatcher.matches()) {
                    //说明与要求的输入格式不匹配
                    msg = "身份证号码格式不正确";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageChange.jsp").forward(request, response);
                    return;
                }
                Room room = null;
                room = roomService.findByRoomNumber(Integer.valueOf(roomNumber));
                if (room == null) {
                    msg = "该房间不存在或已被入住";
                    request.setAttribute("msg", msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION + "/userMessageChange.jsp").forward(request, response);
                    return;
                }
            }
            chain.doFilter(request, response);
            return;
        }
        chain.doFilter(request, response);
    }
}
